{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 13.3 \u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u5982\u4f55\u80fd\u591f\u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\uff08\u4f4d\u4e8esys.argv\u4e2d\uff09"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "argparse \u6a21\u5757\u53ef\u88ab\u7528\u6765\u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\u3002\u4e0b\u9762\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\u6f14\u793a\u4e86\u6700\u57fa\u672c\u7684\u7528\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# search.py\n'''\nHypothetical command-line tool for searching a collection of\nfiles for one or more text patterns.\n'''\nimport argparse\nparser = argparse.ArgumentParser(description='Search some files')\n\nparser.add_argument(dest='filenames',metavar='filename', nargs='*')\n\nparser.add_argument('-p', '--pat',metavar='pattern', required=True,\n                    dest='patterns', action='append',\n                    help='text pattern to search for')\n\nparser.add_argument('-v', dest='verbose', action='store_true',\n                    help='verbose mode')\n\nparser.add_argument('-o', dest='outfile', action='store',\n                    help='output file')\n\nparser.add_argument('--speed', dest='speed', action='store',\n                    choices={'slow','fast'}, default='slow',\n                    help='search speed')\n\nargs = parser.parse_args()\n\n# Output the collected arguments\nprint(args.filenames)\nprint(args.patterns)\nprint(args.verbose)\nprint(args.outfile)\nprint(args.speed)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8be5\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5982\u4e0b\u4f7f\u7528\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 search.py -h\nusage: search.py [-h] [-p pattern] [-v] [-o OUTFILE] [--speed {slow,fast}]\n                 [filename [filename ...]]\n\nSearch some files\n\npositional arguments:\n  filename\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -p pattern, --pat pattern\n                        text pattern to search for\n  -v                    verbose mode\n  -o OUTFILE            output file\n  --speed {slow,fast}   search speed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u90e8\u5206\u6f14\u793a\u4e86\u7a0b\u5e8f\u4e2d\u7684\u6570\u636e\u90e8\u5206\u3002\u4ed4\u7ec6\u89c2\u5bdfprint()\u8bed\u53e5\u7684\u6253\u5370\u8f93\u51fa\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 search.py foo.txt bar.txt\nusage: search.py [-h] -p pattern [-v] [-o OUTFILE] [--speed {fast,slow}]\n                 [filename [filename ...]]\nsearch.py: error: the following arguments are required: -p/--pat\n\nbash % python3 search.py -v -p spam --pat=eggs foo.txt bar.txt\nfilenames = ['foo.txt', 'bar.txt']\npatterns  = ['spam', 'eggs']\nverbose   = True\noutfile   = None\nspeed     = slow\n\nbash % python3 search.py -v -p spam --pat=eggs foo.txt bar.txt -o results\nfilenames = ['foo.txt', 'bar.txt']\npatterns  = ['spam', 'eggs']\nverbose   = True\noutfile   = results\nspeed     = slow\n\nbash % python3 search.py -v -p spam --pat=eggs foo.txt bar.txt -o results \\\n             --speed=fast\nfilenames = ['foo.txt', 'bar.txt']\npatterns  = ['spam', 'eggs']\nverbose   = True\noutfile   = results\nspeed     = fast"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u9009\u9879\u503c\u7684\u8fdb\u4e00\u6b65\u5904\u7406\u7531\u7a0b\u5e8f\u6765\u51b3\u5b9a\uff0c\u7528\u4f60\u81ea\u5df1\u7684\u903b\u8f91\u6765\u66ff\u4ee3 print() \u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "argparse \u6a21\u5757\u662f\u6807\u51c6\u5e93\u4e2d\u6700\u5927\u7684\u6a21\u5757\u4e4b\u4e00\uff0c\u62e5\u6709\u5927\u91cf\u7684\u914d\u7f6e\u9009\u9879\u3002\n\u672c\u8282\u53ea\u662f\u6f14\u793a\u4e86\u5176\u4e2d\u6700\u57fa\u7840\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u5e2e\u52a9\u4f60\u5165\u95e8\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\uff0c\u4f60\u9996\u5148\u8981\u521b\u5efa\u4e00\u4e2a ArgumentParser \u5b9e\u4f8b\uff0c\n\u5e76\u4f7f\u7528 add_argument() \u65b9\u6cd5\u58f0\u660e\u4f60\u60f3\u8981\u652f\u6301\u7684\u9009\u9879\u3002\n\u5728\u6bcf\u4e2a add_argument() \u8c03\u7528\u4e2d\uff0cdest \u53c2\u6570\u6307\u5b9a\u89e3\u6790\u7ed3\u679c\u88ab\u6307\u6d3e\u7ed9\u5c5e\u6027\u7684\u540d\u5b57\u3002\nmetavar \u53c2\u6570\u88ab\u7528\u6765\u751f\u6210\u5e2e\u52a9\u4fe1\u606f\u3002action \u53c2\u6570\u6307\u5b9a\u8ddf\u5c5e\u6027\u5bf9\u5e94\u7684\u5904\u7406\u903b\u8f91\uff0c\n\u901a\u5e38\u7684\u503c\u4e3a store ,\u88ab\u7528\u6765\u5b58\u50a8\u67d0\u4e2a\u503c\u6216\u5c06\u591a\u4e2a\u53c2\u6570\u503c\u6536\u96c6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\n\u4e0b\u9762\u7684\u53c2\u6570\u6536\u96c6\u6240\u6709\u5269\u4f59\u7684\u547d\u4ee4\u884c\u53c2\u6570\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u5728\u672c\u4f8b\u4e2d\u5b83\u88ab\u7528\u6765\u6784\u9020\u4e00\u4e2a\u6587\u4ef6\u540d\u5217\u8868\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parser.add_argument(dest='filenames',metavar='filename', nargs='*')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u53c2\u6570\u6839\u636e\u53c2\u6570\u662f\u5426\u5b58\u5728\u6765\u8bbe\u7f6e\u4e00\u4e2a Boolean \u6807\u5fd7\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parser.add_argument('-v', dest='verbose', action='store_true',\n                    help='verbose mode')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u5355\u72ec\u503c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parser.add_argument('-o', dest='outfile', action='store',\n                    help='output file')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u53c2\u6570\u8bf4\u660e\u5141\u8bb8\u67d0\u4e2a\u53c2\u6570\u91cd\u590d\u51fa\u73b0\u591a\u6b21\uff0c\u5e76\u5c06\u5b83\u4eec\u8ffd\u52a0\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u53bb\u3002\nrequired \u6807\u5fd7\u8868\u793a\u8be5\u53c2\u6570\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u3002-p \u548c --pat \u8868\u793a\u4e24\u4e2a\u53c2\u6570\u540d\u5f62\u5f0f\u90fd\u53ef\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parser.add_argument('-p', '--pat',metavar='pattern', required=True,\n                    dest='patterns', action='append',\n                    help='text pattern to search for')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4e0b\u9762\u7684\u53c2\u6570\u8bf4\u660e\u63a5\u53d7\u4e00\u4e2a\u503c\uff0c\u4f46\u662f\u4f1a\u5c06\u5176\u548c\u53ef\u80fd\u7684\u9009\u62e9\u503c\u505a\u6bd4\u8f83\uff0c\u4ee5\u68c0\u6d4b\u5176\u5408\u6cd5\u6027\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parser.add_argument('--speed', dest='speed', action='store',\n                    choices={'slow','fast'}, default='slow',\n                    help='search speed')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u53c2\u6570\u9009\u9879\u88ab\u6307\u5b9a\uff0c\u4f60\u5c31\u53ef\u4ee5\u6267\u884c parser.parse() \u65b9\u6cd5\u4e86\u3002\n\u5b83\u4f1a\u5904\u7406 sys.argv \u7684\u503c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\u5b9e\u4f8b\u3002\n\u6bcf\u4e2a\u53c2\u6570\u503c\u4f1a\u88ab\u8bbe\u7f6e\u6210\u8be5\u5b9e\u4f8b\u4e2d add_argument() \u65b9\u6cd5\u7684 dest \u53c2\u6570\u6307\u5b9a\u7684\u5c5e\u6027\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u5f88\u591a\u79cd\u5176\u4ed6\u65b9\u6cd5\u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\u3002\n\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u624b\u52a8\u7684\u5904\u7406 sys.argv \u6216\u8005\u4f7f\u7528 getopt \u6a21\u5757\u3002\n\u4f46\u662f\uff0c\u5982\u679c\u4f60\u91c7\u7528\u672c\u8282\u7684\u65b9\u5f0f\uff0c\u5c06\u4f1a\u51cf\u5c11\u5f88\u591a\u5197\u4f59\u4ee3\u7801\uff0c\u5e95\u5c42\u7ec6\u8282 argparse \u6a21\u5757\u5df2\u7ecf\u5e2e\u4f60\u5904\u7406\u4e86\u3002\n\u4f60\u53ef\u80fd\u8fd8\u4f1a\u78b0\u5230\u4f7f\u7528 optparse \u5e93\u89e3\u6790\u9009\u9879\u7684\u4ee3\u7801\u3002\n\u5c3d\u7ba1 optparse \u548c argparse \u5f88\u50cf\uff0c\u4f46\u662f\u540e\u8005\u66f4\u5148\u8fdb\uff0c\u56e0\u6b64\u5728\u65b0\u7684\u7a0b\u5e8f\u4e2d\u4f60\u5e94\u8be5\u4f7f\u7528\u5b83\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}